var remove = require('unordered-array-remove')

//  Efficiently remove an element from an unordered array without doing a splice
// unordered-array-remove

// Removed an element from array by keeping order of elements inside array
// npm install --save orderd-array-remove

var list = ['a', 'b', 'c', 'd', 'e']
console.log( remove(list, 2) ) // remove 'c'
console.log(list) // returns ['a', 'b', 'e', 'd'] (no 'c')



console.log( remove(list, 1) ) 
console.log(list) 

console.log('-----------------------------------------------------')

var list = ['a', 'b', 'c', 'd', 'e']
console.log( unorderedArrayRemove(list, 2) ) // remove 'c'
console.log(list) // returns ['a', 'b', 'e', 'd'] (no 'c')


console.log( unorderedArrayRemove(list, 1) ) 
console.log(list) 



/**
 * 不保持顺序的删除数组的某个索引的元素
 * @param arr
 * @param idx
 */
function  unorderedArrayRemove(arr, idx) {
  var len = arr.length
  if(idx <0 || idx>=len) return

  var last = arr.pop()

  if(idx < arr.length) {
    var tmp = arr[idx]
    arr[idx] = last
    return tmp
  }
  // 刚好删除的是最后一个元素
  return last
}

